#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

int main()
{

  int pid[2]={0};
  if(pipe(pid)<0)
  {
    perror("pipe fail\n");
    return 1;
  }

 // printf("%d,%d\n",pid[0],pid[1]);
  
//子进程,子进程进行数据写入
//子进程关闭读端

  pid_t id=fork();
  if(id < 0)
  {
    perror("fork error");
    return 2;
  }
  else if(id == 0)
  {
    //子进程，子进程进行文件的写

    close(pid[0]);
    const char* msg="hello yjd";
    char a = 'a';
    int count = 0;
    while(1)
    {
    
    write(pid[1],&a,1);
    count++;
    printf("count:%d \n",count);
    }
    close(pid[1]);
    exit(0);
  }
  else
  {
    //父进程，父进程进程数据读取                                                                                                                   
    //父进程关闭写端     
    close(pid[1]);
    
    char buff[2]={0};
    while(1)
    {
    sleep(5);
   ssize_t size = read(pid[0],buff,sizeof(buff)-1);
   if(size > 0)
   {
     buff[size]=0;
     printf("parent get message from child: %s\n",buff);
   }
   else if(size == 0)
   {
     printf("child quit\n");
     break;
  }
  else
  {
   perror("read fail\n");
   break;
  }
 // close(pid[0]);
   }
   } 
   return 0;
   }
